package com.bdqn;

import com.bdqn.pojo.User;
import com.bdqn.pojo.UserVo;
import com.bdqn.repository.UserRepository;
import com.bdqn.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import javax.annotation.Resource;
import java.util.List;

/**
 * JPA基本使用测试类
 *
 * @author LILIBO
 * @since 2022-01-16
 */
@SpringBootTest
public class UserRepositoryTester {

    @Resource
    private UserRepository userRepository;

    @Resource
    private UserService userService;

    /**
     * 测试保存
     */
    @Test
    public void testSave() {
        // 对象没有设置ID时：新增
        User user = new User("小帅老师", "666666", 1L, 1L);
        userRepository.save(user); // 新增对象
        // 对象有ID属性时：修改
        // user.setUsrId(40L); // save()方法保存对象，不带ID属性表示INSTER，带上ID属性表示UPDATE
        // userRepository.save(user); // 修改对象
    }

    /**
     * 测试获取
     */
    @Test
    public void testGet() {
        User user = userRepository.findById(40L).get();
        System.out.println(user.toString());
    }

    /**
     * 测试删除
     */
    @Test
    public void testDelete() {
        userRepository.deleteById(40L);
        System.out.println("删除成功！");
    }

    /**
     * 测试根据方法名称执行查询
     */
    @Test
    public void testFindByUsrName() {
        List<User> list = userRepository.findByUsrNameLike("小帅");
        for (User user : list) {
            System.out.println(user.toString());
        }
    }

    /**
     * 测试根据方法名称执行查询
     */
    @Test
    public void testFindByUsrNameAndUsrPassword() {
        List<User> list = userRepository.findByUsrNameAndUsrPassword("小帅老师", "111");
        for (User user : list) {
            System.out.println(user.toString());
        }
    }

    /**
     * 测试根据方法名称执行查询
     */
    @Test
    public void testFindByRoleId() {
        List<User> list = userRepository.findByRoleId(1L);
        for (User user : list) {
            System.out.println(user.toString());
        }
    }

    /**
     * 测试根据方法名称执行查询
     */
    @Test
    public void testModifyNameById() {
        int flag = userRepository.modifyNameById("小帅同学", 41L);
        if (flag == 1) {
            System.out.println("修改成功！");
        } else {
            System.out.println("修改失败！");
        }
    }

    /**
     * 测试根据方法名称执行查询
     */
    @Test
    public void testFindUsersByName() {
        List<User> list = userRepository.findUsersByName("小帅同学");
        for (User user : list) {
            System.out.println(user.toString());
        }
    }

    /**
     * 测试分页查询
     */
    @Test
    public void testFindPageByUsrRoleId() {
        int page = 0, size = 2; // 分页要素，页数从 0 开始
        // 控制分页数据的排序，可以选择升序和降序
        Sort sort = Sort.by(Sort.Direction.DESC, "usrId"); // Sort sort = new Sort(Sort.Direction.DESC, "usrId");
        // 控制分页的辅助类，可以设置页码、每页的数据条数、排序等
        Pageable pageable = PageRequest.of(page, size, sort);
        Page<User> userPage = userRepository.findPageByUsrRoleId(2L, pageable);

        System.out.println("总记录数： " + userPage.getTotalElements());
        System.out.println("总页数： " + userPage.getTotalPages());
        System.out.println("当前页数： " + (userPage.getNumber() + 1));
        System.out.println("每页记录数： " + userPage.getSize());
        System.out.println("当前页记录数： " + userPage.getNumberOfElements());
        for (User user : userPage.getContent()) {
            System.out.println(user.toString());
        }
    }

    /**
     * 测试关联查询
     */
    @Test
    public void testGetUserVo() {
        // 测试多表联接查询
        UserVo userVo = userRepository.getUserVo(2L);
        System.out.println("UserVo usrName: " + userVo.getUsrName());
        System.out.println("UserVo roleName: " + userVo.getRoleName());
    }

}
